//
//  STLabel.m
//  lover
//
//  Created by stoneobs on 16/5/5.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

#import "STLabel.h"

@interface STLabel()
@property(nonatomic, weak) NSTimer                     *timer;/**<  */
@property(nonatomic, assign) NSInteger                  animation_number;/**< 数字记录 */
@property(nonatomic, assign) NSInteger                  animation_finsh_number;/**< 最终结果  */
@property(nonatomic, assign) NSInteger                  during;/**< 动画 时长  */
@property(nonatomic, weak) NSTimer                     *cicle_timer;/**<  */
@property(nonatomic, assign) NSInteger                  cicle_during;/**< 动画 时长  */
@property(nonatomic, assign) BOOL                  leftToRight;/**<从左  到右还是上 到  下 */
@end
@implementation STLabel
-(instancetype)initWithFrame:(CGRect)frame
                        text:(NSString *)text
                   textColor:(UIColor *)textColor
                        font:(CGFloat)font
                 isSizetoFit:(BOOL)isSizetoFit
               textAlignment:(NSTextAlignment)textAlignment
{
    if (self == [super initWithFrame:frame]) {
        self.numberOfLines = 0;
        self.textAlignment = textAlignment;
        self.text = text;
        self.textColor = textColor;
        self.font = [UIFont systemFontOfSize:font];
        if (isSizetoFit) {
            [self sizeToFit];
        }
    }
    return self;
}
- (void)setText:(NSString *)text{
    text = [text stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
    [super setText:text];
}
- (void)stoneobs_fun_st_loadH5String:(NSString *)h5String{
    NSAttributedString * stoneobs_temp_attrStr = [[NSAttributedString alloc] initWithData:[h5String dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    self.attributedText = stoneobs_temp_attrStr;
}
- (void)stoneobs_fun_st_autoadjustTextWitdh{
    NSString * stoneobs_temp_str = self.text ;
    CGRect stoneobs_temp_rect = [stoneobs_temp_str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.font.pointSize]} context:nil];
    self.width =  stoneobs_temp_rect.size.width;
}
- (void)stoneobs_fun_st_autoadjustTextHeight{
    NSString * stoneobs_temp_str = self.text ;
    CGRect stoneobs_temp_rect = [stoneobs_temp_str boundingRectWithSize:CGSizeMake(self.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.font.pointSize]} context:nil];
    self.height =  stoneobs_temp_rect.size.height;
}
- (void)stoneobs_fun_st_beginNumAnimation:(NSInteger)num during:(NSInteger)during{
    
    self.animation_number  = 0;
    self.animation_finsh_number = num;
    self.during = during;
    self.text = @"0";
    [self.timer invalidate];
    NSTimer * timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(stoneobs_action_timerAction) userInfo:nil repeats:YES];
    self.timer = timer;
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
//从左到右  或者 从上到下 轮播动画
- (void)stoneobs_fun_st_beginCircleAnimaionWithText:(NSString*)text  leftToRight:(BOOL)leftToRight{
    [self.contentLable removeFromSuperview];
    self.clipsToBounds = YES;
    self.leftToRight =  leftToRight;
    self.contentLable = [[STLabel alloc] initWithFrame:self.bounds
                                                    text:text
                                               textColor:self.textColor
                                                    font:self.font.pointSize
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    if (self.leftToRight) {
        [self.contentLable stoneobs_fun_st_autoadjustTextWitdh];
    }else{
        [self.contentLable stoneobs_fun_st_autoadjustTextHeight];
    }
    if(self.contentLable.width < self.width){
        return;
    }
    self.text = @"";
    [self addSubview:self.contentLable];
    
    [self.cicle_timer invalidate];
    NSTimer * cicle_timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(circlestoneobs_action_timerAction) userInfo:nil repeats:YES];
    self.cicle_timer = cicle_timer;
    [[NSRunLoop mainRunLoop]addTimer:cicle_timer forMode:NSRunLoopCommonModes];
}
#pragma mark --Action Method
- (void)stoneobs_action_timerAction{
    NSInteger stoneobs_temp_ereryNum = self.animation_finsh_number / (_during/0.01);
    NSInteger stoneobs_temp_current = self.text.integerValue;
    NSInteger stoneobs_temp_addNum =  stoneobs_temp_current +  stoneobs_temp_ereryNum;
    if (stoneobs_temp_addNum >= self.animation_finsh_number ) {
        stoneobs_temp_addNum = self.animation_finsh_number;
        [self.timer invalidate];
        self.timer = nil;
    }
    self.text = @(stoneobs_temp_addNum).description;
}
- (void)circlestoneobs_action_timerAction{
    
    if (self.leftToRight) {
        self.contentLable.left = self.contentLable.left - 0.2;
        if (self.contentLable.right < 0) {
            self.contentLable.left = 0;
        }
    }else{
        self.contentLable.top = self.contentLable.top - 0.2;
        if (self.contentLable.bottom < 0) {
            self.contentLable.top = 0;
        }
    }

}
@end
